import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from '@/router/routes'
import store from '@/store'

Vue.use(VueRouter)

const router = new VueRouter({
    routes
})

const peromissionList = ['user']

router.beforeEach((to, from, next) => {
    const isLogin = store.state.user.user.isLogin
    if (peromissionList.includes(to.name)) {
        if (isLogin) {
            next()
        } else {
            next('/login')
        }
    } else {
        next()
    }
})
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
    return originalPush.call(this, location).catch(err => err)
}

export default router
